package com.ruoyi.exportCustomer.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.mapper.BaseMapperPlus;
import com.ruoyi.exportCustomer.domain.ExportLog;
import com.ruoyi.exportCustomer.domain.LogsInfo;
import com.ruoyi.exportCustomer.domain.vo.ExportLogVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 导入记录Mapper接口
 *
 * @author ruoyi
 * @date 2023-08-23
 */
public interface ExportLogMapper extends BaseMapperPlus<ExportLogMapper, ExportLog, ExportLogVo> {

    @Select("<script> " +
            " select a.id, a.tel,a.level, a.`status`,a.name,a.bz,a.csd,a.lb,a.sex,a.errMsg, a.flag,( CASE WHEN c.id IS NULL THEN 0 ELSE 1 END ) source,\n"
            + "\t( CASE WHEN c.id IS NULL THEN b.NAME ELSE c.NAME END ) customerName,\n"
            + "\t( CASE WHEN c.id IS NULL THEN b.remark ELSE c.bz END ) customerBz,\n"
            + "\t(\n"
            + "\tCASE\n"
            + "\t\t\tWHEN c.id IS NULL THEN\n"
            + "\t\t\t'' ELSE (\n"
            + "\t\t\tCASE\n"
            + "\t\t\t\t\t\n"
            + "\t\t\t\t\tWHEN c.distribute_dept_id != - 1 THEN\n"
            + "\t\t\t\t\t( SELECT dd.dept_name FROM sys_dept dd WHERE dd.dept_id = c.distribute_dept_id ) ELSE '' \n"
            + "\t\t\t\tEND \n"
            + "\t\t\t\t) \n"
            + "\t\t\tEND \n"
            + "\t\t\t) deptName,\n"
            + "\t\t\t(\n"
            + "\t\t\tCASE\n"
            + "\t\t\t\t\t\n"
            + "\t\t\t\t\tWHEN c.id IS NULL THEN\n"
            + "\t\t\t\t\t'' ELSE (\n"
            + "\t\t\t\t\tCASE\n"
            + "\t\t\t\t\t\t\t\n"
            + "\t\t\t\t\t\t\tWHEN c.distribute_dept_id != - 1 THEN\n"
            + "\t\t\t\t\t\t\t( SELECT uu.nick_name FROM sys_user uu WHERE uu.user_id = c.task_id ) ELSE '' \n"
            + "\t\t\t\t\t\tEND \n"
            + "\t\t\t\t\t\t) \n"
            + "\t\t\t\t\tEND \n"
            + "\t\t\t\t\t) distributeUser " +
            "  FROM ${tabName} a " +
            " \t\t\t\t\tLEFT JOIN total_comster b ON a.tel = b.mobile\n"
            + "\t\t\t\t\tLEFT JOIN t_customer c ON a.tel = c.tel \n"
            + "\t\t\t\t\tAND c.deleted = 0 " +
            " where 1 = 1  " +
            " <if test=\"flag == '1'.toString()\">" +
            "   and flag != 0 " +
            " </if> " +
            " <if test=\"flag == '0'.toString()\">" +
            "   and flag = 0 " +
            " </if> " +
            " <if test=\"name != null and name != ''\">" +
            "	 and  a.name like concat('%',#{name},'%') " +
            " </if> " +
            " <if test=\"type != null and type != ''\">" +
            "	 and  a.flag = #{type} " +
            " </if> " +
            " <if test=\"tel != null and tel != ''\">" +
            "	 and  a.tel like concat('%',#{tel},'%') " +
            " </if> " +
            " <if test=\"level != null and level != ''\">" +
            "	 and  a.level = #{level} " +
            " </if> " +
            " <if test=\"source == '1'.toString()\">" +
            "	 and c.id IS not NULL" +
            " </if> " +
            " <if test=\"source == '0'.toString()\">" +
            "	 and c.id IS NULL" +
            " </if> " +
            " </script>")
    Page<LogsInfo> selectListByPage(Page page, @Param("tabName") String tableName, @Param("name") String name,
            @Param("tel") String tel, @Param("flag") String flag, @Param("type") String type,
            @Param("level") String level, @Param("source") String source);

    @Insert("insert into t_export_log (pc,sucess_cnt,error_cnt,tab_name,create_by,create_time,total) " +
            " select #{pc},count(case when a.flag = 0 then 1 end), count(case when a.flag  != 0 then 1 end), #{tabName}, #{createby}, now(), count(1) from ${tabName} a")
    Integer importTotalCustomer(@Param("tabName") String tableName, @Param("pc") String pc,
            @Param("createby") String createby);

    /**
     * 保存手机号+批次信息
     */
    @Insert("insert into tmp_custorm_bath_info(tel, bath_log, flag, createBy, createTime) select tel, concat('', #{bathLog} , ''), flag, createBy, createTime from ${tableName}")
    void insertBathLog(@Param("tableName") String tableName, @Param("bathLog") String bathLog);

    @Insert("insert into custorm_tel_source_record(tel, source, flag, createBy, createTime) select tel, #{source}, flag, createBy, createTime from ${tableName}")
    void insertSource(@Param("tableName") String tableName, @Param("source") String source);

    @Update("UPDATE t_customer a, ( SELECT GROUP_CONCAT( DISTINCT bath_log order by createTime desc ) bathLog, tel FROM tmp_custorm_bath_info GROUP BY tel ) b\n"
            + "set a.batch_logo = b.bathLog where a.tel = b.tel")
    void updateCustomerBatchLogo();

    @Update("UPDATE total_comster a, ( SELECT GROUP_CONCAT( DISTINCT bath_log order by createTime desc ) bathLog, tel FROM tmp_custorm_bath_info GROUP BY tel ) b\n"
            + "set a.batch_logo = b.bathLog where a.mobile = b.tel")
    void updateTotalCustomerBatchLogo();
}
